package com.uroad.jilinpda.modle;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;

public class CPCEF02EF04InfoMDL implements Serializable {

    private String EF02;
    private String EF04;
    private String mediaNo;//cpc卡ID
    private int accumulatedAmount;//本省累计金额（分）
    private int mileageBefore;//本省累计里程（米）
    private String provincialEntrance;
    private String newGantry;//最新门架
    private String newGantryPassTime;//最新门架通行时间
    private List<EF04InfoMDL> ef04List;
    private SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
    private boolean isAllFitSuccessful = true;//拟合成功
    private long allFeeWithOutLN;//除辽宁外所有省累计金额
    private int provincesNumber;//通行省份个数
    private int gantryNum;//本省门架个数
    private int writeGantryNum;//过站信息中写入的门架数量
    private String writeGantryInfo;//过站信息中写入的门架信息
    private String gantryPassInfo = "";
    private String gantryProPassInfo = "";//本省通行计费信息
    private int lastMileage = 0;//最新门架里程
    private int lastAmount = 0;//最新门架金额

    public String getProvincialEntrance() {
        return provincialEntrance;
    }

    public void setProvincialEntrance(String provincialEntrance) {
        this.provincialEntrance = provincialEntrance;
    }

    public String getWriteGantryInfo() {
        return writeGantryInfo == null ? "" : writeGantryInfo;
    }

    public void setWriteGantryInfo(String writeGantryInfo) {
        this.writeGantryInfo = writeGantryInfo;
    }

    public int getLastMileage() {
        return lastMileage;
    }

    public void setLastMileage(int lastMileage) {
        this.lastMileage = lastMileage;
    }

    public int getLastAmount() {
        return lastAmount;
    }

    public void setLastAmount(int lastAmount) {
        this.lastAmount = lastAmount;
    }

    public int getMileageBefore() {
        return mileageBefore;
    }

    public void setMileageBefore(int mileageBefore) {
        this.mileageBefore = mileageBefore;
    }

    public int getWriteGantryNum() {
        return writeGantryNum;
    }

    public void setWriteGantryNum(int writeGantryNum) {
        this.writeGantryNum = writeGantryNum;
    }

    public int getGantryNum() {
        return gantryNum;
    }

    public void setGantryNum(int gantryNum) {
        this.gantryNum = gantryNum;
    }

    public int getProvincesNumber() {
        return provincesNumber;
    }

    public void setProvincesNumber(int provincesNumber) {
        this.provincesNumber = provincesNumber;
    }

    public long getAllFeeWithOutLN() {
        return allFeeWithOutLN;
    }

    public void setAllFeeWithOutLN(long allFeeWithOutLN) {
        this.allFeeWithOutLN = allFeeWithOutLN;
    }

    public boolean isAllFitSuccessful() {
        return isAllFitSuccessful;
    }

    public void setAllFitSuccessful(boolean allFitSuccessful) {
        isAllFitSuccessful = allFitSuccessful;
    }

    public List<EF04InfoMDL> getEf04List() {
        return ef04List;
    }

    public void setEf04List(List<EF04InfoMDL> ef04List) {
        this.ef04List = ef04List;
    }

    public String getNewGantryPassTime() {
        return newGantryPassTime == null ? "" : newGantryPassTime;
    }

    public void setNewGantryPassTime(String newGantryPassTime) {
        this.newGantryPassTime = newGantryPassTime;
    }

    public String getNewGantry() {
        return newGantry;
    }

    public void setNewGantry(String newGantry) {
        this.newGantry = newGantry;
    }

    public int getAccumulatedAmount() {
        return accumulatedAmount;
    }

    public void setAccumulatedAmount(int accumulatedAmount) {
        this.accumulatedAmount = accumulatedAmount;
    }

    public String getMediaNo() {
        return mediaNo;
    }

    public void setMediaNo(String mediaNo) {
        this.mediaNo = mediaNo;
    }

    public String getEF02() {
        return EF02 == null ? "" : EF02;
    }
    /**0101000138001B201801585EEB1F0D1801585EEB1F0D000138001B2003189876180158180158000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF*/
    public void setEF02(String EF02) {
        this.EF02 = EF02;
        this.provincesNumber = Integer.parseInt(EF02.substring(0,2), 16);
        this.gantryNum = Integer.parseInt(EF02.substring(2,4), 16);
        this.accumulatedAmount = Integer.parseInt(EF02.substring(4,10), 16);
        this.mileageBefore = Integer.parseInt(EF02.substring(10,16), 16);
        this.provincialEntrance = EF02.substring(16,22);
        this.newGantry = EF02.substring(30,36);

        int num = Integer.parseInt(newGantry.substring(0,2),16);//十六进制转十进制
        String gantryTow = Integer.toBinaryString(num);
        String gantryTowAll = String.format("%08d", Integer.parseInt(gantryTow));
        if (!"000110".equals(gantryTowAll.substring(0,6))){//非辽宁门架省份数加一
            provincesNumber++;
        }

        format.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        this.newGantryPassTime = format.format(Long.parseLong(EF02.substring(36, 44), 16) * 1000);
        this.writeGantryNum = Integer.parseInt(EF02.substring(56, 58), 16);
        if (writeGantryNum > 1) {//过站信息中写入的门架数量
            writeGantryInfo = EF02.substring(58, 58+writeGantryNum * 6);
        }
        this.lastAmount = Integer.parseInt(EF02.substring(44, 50), 16);
        this.lastMileage = Integer.parseInt(EF02.substring(50, 56), 16);
    }

    public String getGantryPassInfo() {
        return gantryPassInfo == null ? "" : gantryPassInfo;
    }

    public void setGantryPassInfo(String gantryPassInfo) {
        this.gantryPassInfo = gantryPassInfo;
    }

    public String getGantryProPassInfo() {
        return gantryProPassInfo == null ? "" : gantryProPassInfo;
    }

    public void setGantryProPassInfo(String gantryProPassInfo) {
        this.gantryProPassInfo = gantryProPassInfo;
    }

    public String getEF04() {
        return EF04 == null ? "" : EF04;
    }

    public void setEF04(String EF04) {
        this.EF04 = EF04;
        ef04List = new ArrayList<EF04InfoMDL>();
        isAllFitSuccessful = true;
        for (int i = 0; i < EF04.length() / 46; i++) {
            try {
                String cpc = EF04.substring(i * 46, i * 46 + 46);//每段23字节 46位长度
                if (!"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF".equalsIgnoreCase(cpc)//没写信息
                        && !"0000000000000000000000000000000000000000000000".equalsIgnoreCase(cpc)) {//没写信息
                    gantryPassInfo += cpc;
                    EF04InfoMDL infoMDL = new EF04InfoMDL();
                    //1字节省份行政区域编码
                    infoMDL.setEf0400(cpc.substring(0, 2));
                    //1字节本省ETC门架通行个数
                    infoMDL.setEf0401(cpc.substring(2, 4));
                    //3字节本省累计计费金额
                    infoMDL.setEf0402(String.valueOf(Long.parseLong(cpc.substring(4, 10), 16)));
                    //3字节本省累计计费里程（米）
                    infoMDL.setEf0405(String.valueOf(Long.parseLong(cpc.substring(10, 16), 16)));
                    //3字节本省入口门架信息
                    infoMDL.setEf0408(cpc.substring(16, 22));
                    //4字节本省入口门架通行时间（UNIX时间）
                    infoMDL.setEf0411((Long.parseLong(cpc.substring(22, 30), 16) * 1000)+"");
                    infoMDL.setEf0411HEX(cpc.substring(22, 30));
                    //3字节最新ETC门架编码
                    infoMDL.setEf0415(cpc.substring(30, 36));
                    //4字节最新通行时间
                    infoMDL.setEf0418((Long.parseLong(cpc.substring(36, 44), 16) * 1000)+"");
                    //1字节最新ETC门架拟合成功/失败标识（00 成功 01 失 败）
                    infoMDL.setEf0422("00".equalsIgnoreCase(cpc.substring(44, 46)) ? "成功" : "失败");

                    ef04List.add(infoMDL);
                    if (!"21".equals(cpc.substring(0, 2))){//除辽宁外所有省是否是省界门架并拟合成功
                        int num = Integer.parseInt(cpc.substring(30, 36).substring(0,2),16);//十六进制转十进制
                        String gantryTow = Integer.toBinaryString(num);
                        String gantryTowAll = String.format("%08d", Integer.parseInt(gantryTow));
                        if ("0".equals(gantryTowAll.substring(6,7))||"01".equalsIgnoreCase(cpc.substring(44, 46)))//省界门架并拟合成功
                            isAllFitSuccessful = false;
                        allFeeWithOutLN += Long.parseLong(cpc.substring(4, 10), 16);//除辽宁外所有省累计金额
                    }else{
                        gantryProPassInfo = cpc;
                    }

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
